/**
 *
 *      ___                       ___
 *     /\  \          ___        /\  \
 *    /::\  \        /\  \      /::\  \
 *   /:/\:\  \       \:\  \    /:/\ \  \
 *  /:/  \:\  \      /::\__\  _\:\~\ \  \
 * /:/__/ \:\__\  __/:/\/__/ /\ \:\ \ \__\
 * \:\  \  \/__/ /\/:/  /    \:\ \:\ \/__/
 *  \:\  \       \::/__/      \:\ \:\__\
 *   \:\  \       \:\__\       \:\/:/  /
 *    \:\__\       \/__/        \::/  /
 *     \/__/                     \/__/
 *
 * CIS - Clang Indexing Service is a Project Manager & Source code indexing service
 * created so as to simplify the indexing of C/C++ based source code which use
 * standardized build mechanisms.
 *
 **/

#include "../../includes/commands/buildprojectrequest.hxx"
#include "../../includes/projectdirectory.hxx"
#include "../../includes/requestprocessor.hxx"

#include <sstream>

using namespace cis;
typedef Project::ProjectPtr ProjectPtr;

namespace {
Poco::Util::OptionSet getOptions();

static const Poco::Util::OptionSet options = getOptions();

Poco::Util::OptionSet getOptions()
{
    Poco::Util::OptionSet options;
    options.addOption(Poco::Util::Option("projectname", "n", "project name", true,
                                         "projectname", true).repeatable(false));
    return options;
}

std::string buildInitiated = "Build Scheduled!";

std::string getJsonSimple(const std::string& result)
{
    std::stringstream returnval;
    returnval << "{\"result\" : \"" << result << "\", \"error\" : null, \"id\" : null}" << std::endl;
    return returnval.str();
}

static bool registered = RequestProcessor::addCommand<BuildProjectRequest>("BuildProjectRequest");

}

BuildProjectRequest::BuildProjectRequest()
{
}

const Poco::Util::OptionSet& BuildProjectRequest::getOptions()
{
    return options;
}

std::string BuildProjectRequest::execute(const std::string& params)
{
    processOptions(params);
    ProjectPtr proj = ProjectDirectory::instance().getProject(optionValueMap["projectname"]);
    proj->build();
    return getJsonSimple(buildInitiated);
}